package poker;

import org.directwebremoting.ScriptSessions;

import poker.beans.CardBean;
import poker.beans.GameBean;
import poker.beans.PlayerBean;
import poker.beans.PlayerBean.Action;

public class UI {
	
	public static void foldPlayerCards(PlayerBean player) {
		try{
			ScriptSessions.addFunctionCall("foldPlayerCards", player.getId());
		}
		catch(Exception e){
			ScriptSessions.addFunctionCall("alert", "Error at: foldPlayerCards");
		}
	}
	
	public static void resetHtmlForStage(GameBean game) {
		try{
			for(PlayerBean player : game.getPlayers()){
				ScriptSessions.addFunctionCall("clearPlayerData", player.getId());
			}
			removeActivePlayer(game.getActivePlayer());
		}
		catch(Exception e){
			ScriptSessions.addFunctionCall("alert", "Error at: resetHtmlForStage");
		}
	}

	public static void resetHtmlForRound(GameBean game) {
		for(PlayerBean player : game.getPlayers()){
			ScriptSessions.addFunctionCall("clearPlayerHand", player.getId());
		}
		ScriptSessions.addFunctionCall("clearTable");
		resetHtmlForStage(game);
	}

	public static void displayCard(PlayerBean player) {
		int id = player.getId();
		int card = player.getCards().size();		
		ScriptSessions.addFunctionCall("giveCard", id, card);		
	}
	
	public static void isDealer(int dealerId) {
		ScriptSessions.addFunctionCall("isDealer", dealerId);
	}
	
	public static void removeDealer(int dealerId) {
		ScriptSessions.addFunctionCall("removeDealer", dealerId);
	}
	
	public static void hidePlayer(PlayerBean player) {
		// TODO Auto-generated method stub
		
	}
	
	public static void showPlayerCards(PlayerBean player) {		
		int id = player.getId();
		for( int i = 1; i < 3; i++){		
		ScriptSessions.addFunctionCall("showPlayerCards", id, i,
												player.getCard(i - 1).getNumber(),
												player.getCard(i - 1).getSuit());
		}
	}
	
	public static void writeCommunityCardToHtml(GameBean game) {
		try{
			CardBean card = game.getCommunityCards().get(game.getCommunityCardIndex());
			ScriptSessions.addFunctionCall("writeCommunityCardToHtml",
											game.getCommunityCardIndex(),
											card.getSuit(),
											card.getNumber());
		}
		catch(Exception e){
			ScriptSessions.addFunctionCall("alert", "Error at: writeCommunityCardToHtml");
		}
	}
	
	public static void isActivePlayer(PlayerBean player) {
		ScriptSessions.addFunctionCall("isActivePlayer", player.getId());
	}
	
	public static void removeActivePlayer(PlayerBean player) {
		ScriptSessions.addFunctionCall("removeActivePlayer", player.getId());
	}
	
	
	
	public static void displayButtons(GameBean game) {
		boolean check = false;
		boolean call = false;
		boolean raise = false;
		PlayerBean player = game.getActivePlayer();
		int moneyToCall = game.getMaxBet() - player.getBetSize();
		int raiseAmount = game.getSmallBlind() * 2 * game.getBetMultiplier();		
				
		if( game.getMaxBet() == player.getBetSize()) check = true;
		if(player.getMoney() >= moneyToCall && game.getMaxBet() != 0 ) call = true;
		if ( player.getMoney() >= moneyToCall + raiseAmount &&	game.getTimesRaised() < game.getBetLimit()){
			raise = true;			
		}			
			
		ScriptSessions.addFunctionCall("showPlayerButtons", check, call, raise);
	}

	public static void updatePotAmount(int pot) {
		ScriptSessions.addFunctionCall("updatePotAmount", pot);
	}

	public static void updatePlayerData(PlayerBean player) {
		try{
			String action = "";
			if(player.getAction() != Action.NOACTION) action = player.getAction().toString();
			ScriptSessions.addFunctionCall("updatePlayerData",
											player.getId(),
											player.getName(),
											action,
											player.getBetSize(),
											player.getMoney());
		}
		catch(Exception e){
			ScriptSessions.addFunctionCall("alert", "Error at: updatePlayerData");
		}
	}
	
	public static void revealCards(GameBean game){
		for(PlayerBean player : game.getPlayers()){
			showPlayerCards(player);
		}
	}

	public static void displayContinueButton() {
		ScriptSessions.addFunctionCall("continueGame");
	}
	
}
